<?php
/*******************************************************************************
 * Copyright (c) 2010 Eclipse Foundation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *
 *******************************************************************************/

require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/app.class.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/nav.class.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/menu.class.php");
$App = new App();
$Nav = new Nav();
$Menu = new Menu();
include ($App->getProjectCommon());

$pageTitle = "GEF Downloads";

ob_start();

$PR = "gef";
$debug = (isset ( $_GET ["debug"] ) && preg_match ( "/^\d+$/", $_GET ["debug"] ) ? $_GET ["debug"] : - 1);

$buildtypes = array (
		"R" => "Release",
		"S" => "Milestone",
		"I" => "Integration" 
);

$gef_dls = array (
		"GEF Common, Geometry, FX, MVC, Graph, Layout, Zest, DOT, Cloudio" => array (
				"<acronym title=\"Click to download archived All-In-One p2 Update Site\"><img alt=\"Click to download archived All-In-One p2 Update Site\" src=\"/modeling/images/dl-icon-update-zip.gif\"/> <b style=\"color:green\">GEF Update Site</b></acronym>" => "Update"
		)
);

$gef3_dls = array (
		"Draw2D, GEF (MVC) 3.x and Zest 1.x" => array (
				"<acronym title=\"Click to download archived All-In-One p2 Update Site\"><img alt=\"Click to download archived All-In-One p2 Update Site\" src=\"/modeling/images/dl-icon-update-zip.gif\"/> <b style=\"color:green\">GEF-Legacy Update Site</b></acronym>" => "Update"
				//,
				//"<img alt=\"All-In-One SDK Zip\" src=\"/modeling/images/dl-icon-aio-sdk.gif\"/> <b>GEF ALL SDK</b> (Runtime, Source, Examples)" => "ALL" 
		)
		/*,
		"Draw2D 3.x" => array (
				"SDK (Runtime, Source)" => "draw2d-sdk", // GEF-draw2d-sdk-*.zip
				"Runtime" => "draw2d" 
		),
		"GEF (MVC) 3.x + Draw2D 3.x" => array (
				"SDK (Runtime, Source)" => "SDK",
				"Runtime" => "runtime" 
		),
		"GEF (MVC) 3.x" => array (
				"Examples" => "examples" 
		),
		"Zest 1.x + Draw2D 3.x" => array (
				"SDK (Runtime, Source)" => "zest-sdk",
				"Runtime" => "zest" 
		) */
);

/* zips that are allowed to be absent from the downloads page (eg., new ones added mid-stream) */
$extraZips = array (
		"GEF-zest",
		"GEF-zest-sdk",
		"GEF-draw2d",
		"GEF-draw2d-sdk",
		"GEF-examples",
		"GEF-SDK",
		"GEF-runtime",
		"GEF-ALL" 
);

require_once ($_SERVER ["DOCUMENT_ROOT"] . "/gef/downloads/downloads-scripts.php");

$numzips = isset ( $extraZips ) ? 0 - sizeof ( $extraZips ) : 0; // if extra zips (new zips added later), reduce the "required" count when testing a build
if (isset ( $dls [$proj] ) && is_array ( $dls [$proj] )) {
	foreach ( array_keys ( $dls [$proj] ) as $z ) {
		$numzips += sizeof ( $dls [$proj] [$z] );
	}
}

// store an array of paths to hide
$hiddenBuilds = is_readable ( $_SERVER ["DOCUMENT_ROOT"] . "/gef/downloads/hidden.txt" ) ? file ( $_SERVER ["DOCUMENT_ROOT"] . "/gef/downloads/hidden.txt" ) : array ();

// include extras-$PR.php
$files = array (
		$_SERVER ["DOCUMENT_ROOT"] . "/gef/downloads/extras-gef.php" 
);
foreach ( $files as $file ) {
	if (file_exists ( $file )) {
		include_once ($file);
		break;
	}
}

$hadLoadDirSimpleError = 1; // have we echoed the loadDirSimple() error msg yet? if 1, omit error; if 0, echo at most 1 error
$regs = null;
$showAll = (isset ( $_GET ["showAll"] ) && preg_match ( "/^(1)$/", $_GET ["showAll"], $regs ) ? $regs [1] : "0");
$showMax = (isset ( $_GET ["showMax"] ) && preg_match ( "/^(\d+)$/", $_GET ["showMax"], $regs ) ? $regs [1] : "5");

$GEF_DROPS_DIR = getPWD ( "downloads/drops" ); // see scripts.php
$GEF_LEGACY_DROPS_DIR = getPWD ( "downloads/drops/legacy" ); // see scripts.php

$downloadScript = getDownloadScript();

print "<div id=\"midcolumn\">\n";
print "<h2>$pageTitle</h2>\n";
print "<p>All downloads are provided under the terms and conditions of the <a href=\"/legal/epl/notice.php\">Eclipse Foundation Software User Agreement</a> unless otherwise specified.</p>";

print "<h3>Online Update-Sites</h3>";
print "<p>All deployed GEF-Legacy (3.x), GEF4, and GEF (5.x) artifacts can be consumed from our releases, milestones, integration, and CI <a href=\"https://projects.eclipse.org/projects/tools.gef/downloads\">update-sites</a>.</p>";
print "<p><small>Please note that the GEF-Legacy (3.x) update site urls have changed with Neon (see <a href=\"https://bugs.eclipse.org/bugs/show_bug.cgi?id=470240\">bug #470240</a> for details).</small></p>";

// collect which branches (3.9.101, 3.10.0, etc.) of GEF and GEF4 exist in the drop dirs
$branches = array_unique ( array_merge ( loadDirSimple ( $GEF_DROPS_DIR, ".*", "d" ), loadDirSimple ( $GEF_LEGACY_DROPS_DIR, ".*", "d" ), loadDirSimple ( $GEF4_DROPS_DIR, ".*", "d" ) ) );
sort ( $branches );
// $buildtypes is configured in _projectCommon.php to be R, S, I, here statically all build types are created for each branch
$buildTypes = getBuildTypes ( $branches, $buildtypes );

$gefBuilds = getBuildsFromDirs ( $GEF_DROPS_DIR );
$gefLegacyBuilds = getBuildsFromDirs ( $GEF_LEGACY_DROPS_DIR );

print "<h3>Update-Site Archives</h3>";
print "<p>The update-sites provided by individual GEF-Legacy (3.x), GEF4, and GEF (5.x) release, milestone, and integration builds can also be downloaded as .zip archives for offline-installation below.</p>";
print "<p><small>Please note that SDK archives (in runnable form) for GEF-Legacy (3.x) are no longer provided since Mars SR1 (see <a href=\"https://bugs.eclipse.org/bugs/show_bug.cgi?id=470240\">bug #470240</a> for details).</small></p>";

print "<div id=\"midcolumn\">\n";
if (sizeof ( $gefBuilds ) == 0 || sizeof ( $gefLegacyBuilds ) == 0) {
	print "<h4>Builds</h4>\n";
	print "<ul>\n";
	print "<li><i><b>Error!</b></i> No builds found on this server!</li>";
	print "</ul>\n";
}

$c = 0;
foreach ( $buildTypes as $branch => $types ) {
	foreach ( $types as $type => $label ) {
		// Check if a GEF or GEF Legacy build exists
		if ((array_key_exists ( $branch, $gefBuilds ) && array_key_exists ( $type, $gefBuilds [$branch] )) || (array_key_exists ( $branch, $gefLegacyBuilds ) && array_key_exists ( $type, $gefLegacyBuilds [$branch] ))) {
			print "<h4>" . $label . "s</h4>\n";
			print "<ul>\n";
			// output GEF builds
			if (array_key_exists ( $branch, $gefBuilds ) && array_key_exists ( $type, $gefBuilds [$branch] )) {
				showBuilds ( "", $GEF_DROPS_DIR, $branch, $gefBuilds [$branch] [$type], "GEF ", "GEF", $gef_dls, $c );
			}
			// output GEF legacy builds
			if (array_key_exists ( $branch, $gefLegacyBuilds ) && array_key_exists ( $type, $gefLegacyBuilds [$branch] )) {
				showBuilds ( "legacy", $GEF_LEGACY_DROPS_DIR, $branch, $gefLegacyBuilds [$branch] [$type], "GEF-Legacy ", "GEF3", $gef3_dls, $c );
			}
			print "</ul>\n";
		}
	}
}

// output archived GEF builds
if (isset ( $old_gef_rels ) && is_array ( $old_gef_rels ) && sizeof ( $old_gef_rels ) > 0) {
	showArchived ( $old_gef_rels );
}

print "<h3>Translations</h3>";
print "<p>Translations packages can be downloaded from the <a href=\"http://www.eclipse.org/babel/downloads.php\">Babel project downloads page</a> (from GEF 3.4 onwards). Older translation packages (for GEF 3.0 - 3.2) can still be found <a href=\"translations/translation.html\">here</a></p>";
print "</div>\n";

$html = ob_get_contents();
ob_end_clean();

$pageTitle = "GEF Update-Site and SDK Archive Downloads";
$pageKeywords = ""; // TODO: add something here
                    
// Generate the web page
$App->AddExtraHtmlHeader ( '<script src="/gef/downloads/downloads.js" type="text/javascript"></script>' . "\n" ); // ie doesn't understand self closing script tags, and won't even try to render the page if you use one
$App->generatePage ( $theme, $Menu, $Nav, $pageAuthor, $pageKeywords, $pageTitle, $html );
?>
